/**
 * Copyright (C) <2023> <Boundivore> <boundivore@foxmail.com>
 * <p>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Apache License, Version 2.0 for more details.
 * <p>
 * You should have received a copy of the Apache License, Version 2.0
 * along with this program; if not, you can obtain a copy at
 * http://www.apache.org/licenses/LICENSE-2.0.
 */
import React, { createContext, useState, useRef } from 'react';
import { ConfigGroupVo, NodeType } from '@/api/interface';
interface ConfigStateContextType {
	activeTab: string; // 根据实际类型调整
	setActiveTab: React.Dispatch<React.SetStateAction<any>>;
	configPath: string;
	setConfigPath: React.Dispatch<React.SetStateAction<any>>;
	codeVersion: string;
	setCodeVersion: React.Dispatch<React.SetStateAction<any>>;
	configGroupInfo: ConfigGroupVo[]; // 修改配置文件时的分组信息
	setConfigGroupInfo: React.Dispatch<React.SetStateAction<any>>;
	currentGroupIndex: number; //修改配置文件时当前选中的分组index
	setCurrentGroupIndex: React.Dispatch<React.SetStateAction<any>>;
	activeContent: ConfigGroupVo[]; // 修改配置文件时的分组信息
	setActiveContent: React.Dispatch<React.SetStateAction<any>>;
	codeEdit: string;
	setCodeEdit: React.Dispatch<React.SetStateAction<any>>;
	isConfigModified: boolean; // 是否修改过配置文件
	setIsConfigModified: React.Dispatch<React.SetStateAction<any>>;
	currentNodeId: string; // 是否修改过配置文件
	setCurrentNodeId: React.Dispatch<React.SetStateAction<any>>;
	currentNodeInfo: Partial<NodeType>; // 是否修改过配置文件
	setCurrentNodeInfo: React.Dispatch<React.SetStateAction<any>>;
	editorRef: React.MutableRefObject<{
		handleSave: () => string;
	} | null>;
}

export const ConfigStateContext = createContext<ConfigStateContextType | undefined>(undefined);

export const ConfigStateProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
	const [activeTab, setActiveTab] = useState<string>('');
	const [configPath, setConfigPath] = useState<string>('');
	const [codeVersion, setCodeVersion] = useState<string>('');
	const [configGroupInfo, setConfigGroupInfo] = useState<ConfigGroupVo[]>([]);
	const [currentGroupIndex, setCurrentGroupIndex] = useState(0);
	const [activeContent, setActiveContent] = useState<ConfigGroupVo[]>([]);
	const [codeEdit, setCodeEdit] = useState<string>('');
	const [isConfigModified, setIsConfigModified] = useState<boolean>(false);
	const [currentNodeId, setCurrentNodeId] = useState<string>('');
	const [currentNodeInfo, setCurrentNodeInfo] = useState<Partial<NodeType>>({});
	const editorRef = useRef<{ handleSave: () => string } | null>(null);

	return (
		<ConfigStateContext.Provider
			value={{
				activeTab,
				setActiveTab,
				configPath,
				setConfigPath,
				codeVersion,
				setCodeVersion,
				configGroupInfo,
				setConfigGroupInfo,
				currentGroupIndex,
				setCurrentGroupIndex,
				activeContent,
				setActiveContent,
				codeEdit,
				setCodeEdit,
				isConfigModified,
				setIsConfigModified,
				currentNodeId,
				setCurrentNodeId,
				currentNodeInfo,
				setCurrentNodeInfo,
				editorRef
			}}
		>
			{children}
		</ConfigStateContext.Provider>
	);
};
